package jt.example.notificationmanager;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * Created by TaoJn on 4/7/2016.
 */
public class MainActivity extends Activity{

    public static  final String INTENT_STATE = "CURRENT_STATE";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        initStates();
    }

    private void initStates(){
        final String[] stateList = this.getResources().getStringArray(R.array.stateList);
        ArrayAdapter<String> stateArray = new ArrayAdapter<String>(
                MainActivity.this,
                android.R.layout.simple_spinner_item,
                stateList);

        stateArray.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        Spinner spinner =(Spinner)this.findViewById(R.id.spinner);
        spinner.setAdapter(stateArray);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String stateSelected = stateList[position];
                notifyState(stateSelected);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private void notifyState(String selectedState){
        // 单击Notification时， 要运行的Activity.
        Intent notifyIntent = new Intent(MainActivity.this, NotifyActivity.class);
        notifyIntent.putExtra(INTENT_STATE,selectedState);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent appIntent = PendingIntent.getActivity(MainActivity.this, 0, notifyIntent, 0);

        Notification notification = new Notification();
        notification.icon = R.drawable.state;
        notification.tickerText = selectedState;
        notification.defaults = Notification.DEFAULT_SOUND;
        notification.setLatestEventInfo(MainActivity.this, "登陆状态", selectedState, appIntent);

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Service.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notification);
    }
}
